<?php
class Bliss_Form_DbTable extends Bliss_Form
{

	protected $_name;
	protected $_modalClass;
	protected $_table;
	protected $_where;
	protected $_fields;
	protected $_data;
	protected $_defaults;

	public function __construct($table = null, $where = null, array $defaults = null)
	{
		parent::__construct();

		if(isset($this->_modalClass)) {
			$this->setTable(new $this->_modalClass());
		} else {
			$table = isset($this->_name) ? $this->_name : $table;
			if(isset($table)) {
				$this->setTable($table);
			}
		}

		$where = isset($this->_where) ? $this->_where : $where;
		if(isset($where)) {
			$this->where($where);
		}

		$defaults	= isset($this->_defaults) ? $this->_defaults : $defaults;
		if(isset($defaults)) {
			$this->setDefaults($defaults);
		}

		$this->create();
	}

	public function setTable($table)
	{
		if(is_string($table)) {
			$this->setTable(new Zend_Db_Table($table));
		} elseif($table instanceOf Zend_Db_Table_Abstract) {
			$this->_table = $table;
			
		} else {
			throw new Zend_Exception('No table name or instance was provided.');
		}
	}

	public function where($where)
	{
		$this->_where = $where;
		$this->get($this->_where);
	}

	public function setDefaults(array $defaults)
	{
		$this->_defaults	= new Zend_Config($defaults);
	}

	public function create()
	{
		$this->_setupFields();
	}

	protected function _setupFields()
	{
		$info = $this->_table->info();
		foreach($info['metadata'] as $fieldName => $data) {
			$field		= $this->_determineField($data);
			$element	= "Zend_Form_Element_{$field['element']}";

			if(!$field['visible']) continue;
			$this->_fields[$fieldName] = null;
			$this->addElement(new $element($fieldName, array(
				'label' => $field['label'],
				'value' => $field['value'],
				'multiOptions' => $field['values']
			)));
		}
		
		$this->addElement('submit', 'Submit');
	}

	protected function _determineField($data)
	{
		$defaults	= isset($this->_defaults->{$data['COLUMN_NAME']}) ? $this->_defaults->{$data['COLUMN_NAME']}->toArray() : array();
		$values		= $keys = array();
		$element	= 'Text';
		if(preg_match("/^enum\((.*?)\)$/", $data['DATA_TYPE'], $matches)) {
			$str	= str_replace("'", '', $matches[1]);
			$keys	= explode(",", $str);
			$values	= array_map('ucwords', $keys);
			$element = 'Select';
		}

		$field	= array(
			'element'	=> $element,
			'values'	=> count($values) ? array_combine($keys, $values) : null,
			'label'		=> ucwords(str_replace("_", " ", $data['COLUMN_NAME'])),
			'value'		=> isset($this->_data[$data['COLUMN_NAME']]) ? $this->_data[$data['COLUMN_NAME']] : $data['DEFAULT'],
			'visible'	=> $data['IDENTITY'] ? false : true,
			'identity'	=> $data['IDENTITY'],
		);

		return array_merge($field, $defaults);
	}

	public function save($where = null)
	{
		$data	= $this->getFieldValues();
		$where	= isset($this->_where) ? $this->_where : $where;
		if(isset($where)) {
			return $this->_table->update($data, $where);
		} else {
			return $this->_table->insert($data);
		}
	}

	public function getFieldValues()
	{
		foreach($this->_fields as $key => $value) {
			$this->_fields[$key] = $this->{$key}->getValue();
		}

		return $this->_fields;
	}

	public function get($where = null)
	{
		if(isset($where)) {
			$this->_data = $this->_table->fetchRow($where)->toArray();
		} else {
			$this->_data = array();
		}
		
		return $this->_data;
	}
}
?>